/*=============================================================================

  Library: CFS

  Copyright (c) German Cancer Research Center,
    Division of Medical and Biological Informatics

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

=============================================================================*/

#include "CFSPluginFrameworkEvent.h"

#include "CFSPlugin.h"

#include <CFSException.h>

#include <QString>
#include <QDebug>

class CFSPluginFrameworkEventData : public QSharedData
{
public:

  CFSPluginFrameworkEventData(CFSPluginFrameworkEvent::Type type, QSharedPointer<CFSPlugin> plugin, const QString& exc)
    : plugin(plugin), errorString(exc), type(type)
  {

  }

  CFSPluginFrameworkEventData(const CFSPluginFrameworkEventData& other)
    : QSharedData(other), plugin(other.plugin), errorString(other.errorString),
      type(other.type)
  {

  }

  /**
   * Plugin related to the event.
   */
  const QSharedPointer<CFSPlugin>	plugin;

  /**
   * Exception related to the event.
   */
  const QString errorString;

  /**
   * Type of event.
   */
  const CFSPluginFrameworkEvent::Type type;
};

//----------------------------------------------------------------------------
CFSPluginFrameworkEvent::CFSPluginFrameworkEvent()
  : d(0)
{

}

//----------------------------------------------------------------------------
CFSPluginFrameworkEvent::~CFSPluginFrameworkEvent()
{

}

//----------------------------------------------------------------------------
bool CFSPluginFrameworkEvent::isNull() const
{
  return !d;
}

//----------------------------------------------------------------------------
CFSPluginFrameworkEvent::CFSPluginFrameworkEvent(Type type, QSharedPointer<CFSPlugin> plugin, const CFSException &fwException)
  : d(new CFSPluginFrameworkEventData(type, plugin, fwException.what()))
{

}

//----------------------------------------------------------------------------
CFSPluginFrameworkEvent::CFSPluginFrameworkEvent(Type type, QSharedPointer<CFSPlugin> plugin)
  : d(new CFSPluginFrameworkEventData(type, plugin, QString()))
{

}

//----------------------------------------------------------------------------
CFSPluginFrameworkEvent::CFSPluginFrameworkEvent(const CFSPluginFrameworkEvent& other)
  : d(other.d)
{

}

//----------------------------------------------------------------------------
CFSPluginFrameworkEvent& CFSPluginFrameworkEvent::operator=(const CFSPluginFrameworkEvent& other)
{
  d = other.d;
  return *this;
}

//----------------------------------------------------------------------------
QString CFSPluginFrameworkEvent::getErrorString() const
{
  return d->errorString;
}

//----------------------------------------------------------------------------
QSharedPointer<CFSPlugin> CFSPluginFrameworkEvent::getPlugin() const
{
  return d->plugin;
}

//----------------------------------------------------------------------------
CFSPluginFrameworkEvent::Type CFSPluginFrameworkEvent::getType() const
{
  return d->type;
}

//----------------------------------------------------------------------------
QDebug operator<<(QDebug dbg, CFSPluginFrameworkEvent::Type type)
{
  switch (type)
  {
  case CFSPluginFrameworkEvent::FRAMEWORK_STARTED:        return dbg << "FRAMEWORK_STARTED";
  case CFSPluginFrameworkEvent::PLUGIN_ERROR:          return dbg << "PLUGIN_ERROR";
  case CFSPluginFrameworkEvent::PLUGIN_WARNING:        return dbg << "PLUGIN_WARNING";
  case CFSPluginFrameworkEvent::PLUGIN_INFO:           return dbg << "PLUGIN_INFO";
  case CFSPluginFrameworkEvent::FRAMEWORK_STOPPED:        return dbg << "FRAMEWORK_STOPPED";
  case CFSPluginFrameworkEvent::FRAMEWORK_STOPPED_UPDATE: return dbg << "FRAMEWORK_STOPPED_UPDATE";
  case CFSPluginFrameworkEvent::FRAMEWORK_WAIT_TIMEDOUT:  return dbg << "FRAMEWORK_WATI_TIMEDOUT";

  default: return dbg << "unknown plugin framework event type (" << static_cast<int>(type) << ")";
  }
}

//----------------------------------------------------------------------------
QDebug operator<<(QDebug dbg, const CFSPluginFrameworkEvent& event)
{
  if (event.isNull()) return dbg << "NONE";

  CFSPlugin* p = event.getPlugin().data();
  QString err = event.getErrorString();

  dbg.nospace() << event.getType() << " #" << p->getPluginId() << " ("
      << p->getLocation() << ")" << (err.isEmpty() ? "" : " exception: ") << err;
  return dbg.maybeSpace();
}
